<template>
  <section class="content">
    <div class="box box-info">
      <div class="box-header with-border p-tb-n">
        <h3 class="box-title">仓库盘点</h3>
      </div>
      <div class="form-horizontal">
        <div class="box-body">
          <div class="clearfix row">
            <div class="col-sm-12 flex"  style="align-items: initial">
              <label class="control-label bg-gray min-width">仓库位置：</label>
              <div class="inline-block">
                <div class="m-b-20" v-if="userPrivilegeWarehouse.length == 0">
                  <label class="payment">
                    <i class="fa fa-warning"></i>
                    您当前还未添加仓库
                    <router-link to="/v1/goods/category/getList" class="text-red text-bold">【立即添加】</router-link>
                  </label>
                </div>
                <div v-else>
                  <button type="button" class="btn btn-default btn-flat m-r-10 m-b-20 pull-left"
                          @click="chooseWarehouse(null,-1)"
                          :class='[{ "btn-info" :activeClass.storckActive == -1}]'>全部
                  </button>
                  <button v-for="(userPrivilegeWarehouseItem, index) in userPrivilegeWarehouse" type="button"
                          class="btn btn-default btn-flat m-b-20 m-r-10"
                          :class='[{ "btn-info" :index==activeClass.storckActive}]'
                          @click="chooseWarehouse(userPrivilegeWarehouseItem.id,index)">
                    {{userPrivilegeWarehouseItem.name}}
                  </button>
                </div>
              </div>
            </div>
          </div>
          <div class="clearfix row">
            <div class="col-sm-12 flex" style="align-items: initial">
              <label class="control-label bg-gray min-width">盘点状态：</label>

              <div class="inline-block">
                <button v-for="(takeStockStatusItem,index) in takeStockStatus" type="button"
                        class="btn btn-flat btn-default m-r-10 m-b-20"
                        :class='[{ "btn-info" :index==activeClass.inventoryActive}]'
                        @click="chooseTakeStockStatus(takeStockStatusItem.status,index)">{{takeStockStatusItem.name}}
                </button>
              </div>
            </div>
          </div>
          <div class="form-group m-b-n">
            <div class="col-sm-12 flex">
              <label class="min-width bg-gray control-label">输入搜索：</label>
              <div class="inline-block">
                <input v-model="inquireInventory.invNo" type="text" class="form-control pull-left"
                       placeholder="请输入盘点单编号" style="width: 350px;">
                <button class="btn bg-green pull-left m-lr-10" v-if="$_has('sys:whcheck:list')"
                        @click="searchInventoryKey">搜索
                </button>
                <button class="btn btn-danger pull-left m-lr-10" @click="resetInvNo">重置</button>
              </div>
            </div>
          </div>
        </div>
        <!-- /.box-body -->

        <!-- /.box-footer -->
      </div>
    </div>
    <div class="row">
      <div class="col-xs-12">
        <div class="box box-info">
          <div class="box-header with-border">
            <h3 class="box-title">仓库盘点列表</h3>
            <!--<button type="button" class="btn btn-success pull-right m-r-10" v-if="$_has('sys:whcheck:print')">打印盘点单-->
            <!--</button>-->
            <!--<button type="button" class="btn btn-success pull-right m-r-10" v-if="$_has('sys:whcheck:out')">导出</button>-->
            <!--<button type="button" class="btn btn-success pull-right m-r-10" v-if="$_has('sys:whcheck:in')">导入</button>-->
            <!--<button type="button" class="btn btn-success pull-right m-r-10" data-toggle="modal"-->
            <!--data-target="#modal-default2" v-if="$_has('sys:whcheck:addsku')">创建sku盘点单-->
            <!--</button>-->
            <button type="button" class="btn btn-primary pull-right m-r-10" data-toggle="modal"
                    data-target="#modal-default" @click="createStorckOrShelf" v-if="$_has('sys:whcheck:addstock')">
              创建货架/库位盘点单
            </button>
            <button type="button" class="btn btn-primary pull-right m-r-10" data-toggle="modal"
                    data-target="#modal-default" @click="clickWholeWarehouse" v-if="$_has('sys:whcheck:addwh')">创建整仓盘点单
            </button>
          </div>
          <!-- /.box-header -->
          <div class="box-body">
            <table id="example3" class="table table-bordered table-hover">
              <div class="overlay" v-if="show">
                <i class="fa fa-refresh fa-spin"></i>
              </div>
              <thead>
              <tr>
                <!--<th style="width: 50px;">-->
                  <!--<label>-->
                    <!--<input type="checkbox" class="flat-red">-->
                  <!--</label>-->
                <!--</th>-->
                <th style="width: 10%">盘点单编号</th>
                <th>仓库</th>
                <th>进度/盘点总数</th>
                <th>创建时间</th>
                <th>状态</th>
                <th>操作</th>
              </tr>
              </thead>
              <tbody>
              <tr v-if="inventoryLists.length == 0">
                <td colspan="6" class="text-center">没有数据</td>
              </tr>
              <tr v-for="(inventoryListsItem,index) in inventoryLists">
                <!--<td>-->
                  <!--<label>-->
                    <!--<input type="checkbox" class="flat-red">-->
                  <!--</label>-->
                <!--</td>-->
                <td>{{inventoryListsItem.invNo}}</td>
                <td class="text-left" style="padding-left: 1%;">{{inventoryListsItem.whName}}</td>
                <td class="text-right" style="padding-right: 1%;">
                  {{inventoryListsItem.ongoing}}/{{inventoryListsItem.totalNum}}
                </td>
                <td>{{inventoryListsItem.createTime}}</td>
                <td :class='[{ "text-red" :inventoryListsItem.status == 1 || 2}]'>
                  {{analysisTakeStockStatus.get(inventoryListsItem.status)}}
                </td>
                <td>
                  <div class="flex">
                    <span style="flex: 1">
                      <a v-if="inventoryListsItem.status == 1 && $_has('sys:whcheck:play')" class="text-green m-lr-10"
                         data-toggle="modal"
                         data-target="#modal-default3"
                         @click="editorInventory(inventoryListsItem.invNo,inventoryListsItem.checkType)">开始盘点</a>
                      <a v-if="inventoryListsItem.status == 2 && $_has('sys:whcheck:play')" class="text-green m-lr-10"
                         data-toggle="modal"
                         data-target="#modal-default3"
                         @click="editorInventory(inventoryListsItem.invNo,inventoryListsItem.checkType)">继续盘点</a>
                    </span>
                    <span style="flex: 1">
                      <a class="text-green m-lr-10"
                         @click="detailsInventory(inventoryListsItem.invNo,inventoryListsItem.checkType)"
                         data-toggle="modal"
                         data-target="#modal-default3" v-if="$_has('sys:whcheck:detail')">详情</a>
                    </span>
                    <span style="flex: 1">
                      <a v-if="inventoryListsItem.status == 1 && $_has('sys:whcheck:del')" class="text-green m-lr-10"
                         @click="deleteTaks(inventoryListsItem.invNo)">取消</a>
                    </span>
                  </div>
                </td>
              </tr>
              </tbody>
            </table>
            <template class="clearfix">
              <div class="m-t-15 pull-right">
                <pagination :total="total" :current-page='current' :display="display" @pagechange="pagechange"></pagination>
              </div>
            </template>
          </div>
          <!-- /.box-body -->
        </div>
        <!-- /.box -->

      </div>
      <!-- /.col -->
    </div>
    <!-- /.row -->

    <!--</div>-->
    <div class="modal fade" id="modal-default">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title">{{title}}</h4>
          </div>
          <div class="modal-body">
            <div class="form-horizontal max_height">
              <div class="clearfix">
                <div class="form-group">
                  <div v-if="checkType == 1" class="col-sm-6 flex m-b-20">
                    <label for="" class="min-width bg-gray control-label"><sup class="text-red">*</sup>仓库：</label>
                    <div class="inline-block">
                      <div v-if="userPrivilegeWarehouse.length == 0">
                        <label class="payment">
                          <i class="fa fa-warning"></i>
                          您当前还未添加仓库
                          <router-link to="/v1/goods/category/getList" class="text-red text-bold">【立即添加】</router-link>
                        </label>
                      </div>
                      <select v-else name="" class="form-control" v-model="submitWhParam.whCheck.whId"
                              @change="createWholeWarehouse">
                        <option value="">请选择仓库</option>
                        <option v-for="(userPrivilegeWarehouseItem, index) in userPrivilegeWarehouse"
                                :value="userPrivilegeWarehouseItem.id">{{userPrivilegeWarehouseItem.name}}
                        </option>
                      </select>
                    </div>
                  </div>
                  <div v-if="checkType == 2" class="col-sm-6 flex m-b-20">
                    <label for="" class="min-width bg-gray control-label"><sup class="text-red">*</sup>仓库：</label>
                    <div class="inline-block">
                      <div v-if="userPrivilegeWarehouse.length == 0">
                        <label class="payment">
                          <i class="fa fa-warning"></i>
                          您当前还未添加仓库
                          <router-link to="/v1/goods/category/getList" class="text-red text-bold">【立即添加】</router-link>
                        </label>
                      </div>
                      <select v-else name="" class="form-control" v-model="submitWhParam.whCheck.whId"
                              @change="getShelfSelect">
                        <option value="">请选择仓库</option>
                        <option v-for="(userPrivilegeWarehouseItem, index) in userPrivilegeWarehouse"
                                :value="userPrivilegeWarehouseItem.id">{{userPrivilegeWarehouseItem.name}}
                        </option>
                      </select>
                    </div>
                  </div>

                  <div v-if="checkType == 2" class="col-sm-6 flex m-b-20">
                    <label for="" class="min-width bg-gray control-label"><sup class="text-red">*</sup>货架：</label>
                    <div class="inline-block">
                      <select name="" class="form-control" v-model="shelfId" @change="getProductByShelf">
                        <option value="">请选择货架</option>
                        <option v-for="ShelfSelectListItem in shelfSelectList" :value="ShelfSelectListItem.id">
                          {{ShelfSelectListItem.shelfNo}}
                        </option>
                      </select>
                    </div>
                  </div>
                  <div v-if="checkType == 2" class="col-sm-6 flex">
                    <label for="" class="min-width bg-gray control-label">库位：</label>
                    <div class="inline-block">
                      <select name="" class="form-control" v-model="stockId" @change="getProductByStock">
                        <option value="">请选择库位</option>
                        <option :value="stockSelectListItem.id" v-for="stockSelectListItem in stockSelectList">
                          {{stockSelectListItem.stockNo}}
                        </option>
                      </select>
                    </div>
                  </div>
                </div>
                <div class="clearfix">
                  <div class="col-sm-12">
                    <table id="example6" class="table table-bordered table-hover marginNonde">
                      <thead>
                      <tr>
                        <th>商品图片</th>
                        <th>商品信息</th>
                        <th>货架/库位 <span class="text-red">（库存）</span></th>
                        <th>合计</th>
                      </tr>
                      </thead>
                      <tbody>
                      <tr v-if="takeStockInfo.length == 0">
                        <td colspan="4">没有数据</td>
                      </tr>
                      <tr v-else v-for="(takeStockInfoItem,index) in takeStockInfo">
                        <td>
                          <div class="imgW">
                            <img v-if="takeStockInfoItem.goodsPic" :src="takeStockInfoItem.goodsPic" alt="">
                            <img v-else src="@/assets/noPic.png" alt="">
                          </div>
                        </td>
                        <td class="text-left" style="padding-left: 20px;">
                          <label class="m-l-15">
                            <p>
                              <label>商品编码：</label>
                              <span>{{takeStockInfoItem.sku}}</span>
                            </p>
                            <p>
                              <label>商品名称：</label>
                              <span>{{takeStockInfoItem.goodsName}}</span>
                            </p>
                            <p>
                              <label>品牌名称：</label>
                              <span>{{takeStockInfoItem.brandName}}</span>
                            </p>
                          </label>
                        </td>
                        <td>
                          <label class="m-l-15">
                            <p v-for="takeStockInfoStockDetailItem in takeStockInfoItem.stockInfoVOS">
                              <label>{{takeStockInfoStockDetailItem.stockNo}}</label>
                              <span class="text-red">({{takeStockInfoStockDetailItem.stockQty}})</span>
                            </p>
                          </label>
                        </td>
                        <td>
                          {{takeStockInfoItem.countStock}}
                        </td>
                      </tr>
                      </tbody>
                    </table>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-primary" @click="submitWholeWarehouse">保存</button>
            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
          </div>
        </div>
        <!-- /.modal-content -->
      </div>
      <!-- /.modal-dialog -->
    </div>

    <div class="modal fade" id="modal-default3">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title">{{title}}</h4>
          </div>
          <div class="modal-body">
            <div class="form-horizontal max_height">
              <div class="clearfix">
                <div class="form-group">
                  <div class="col-sm-6 flex">
                    <label for="" class="min-width bg-gray control-label">盘点单编号：</label>
                    <label for="" class="control-label">{{editorInvList.whCheckVO.invNo}}</label>
                  </div>
                  <div class="col-sm-6 flex">
                    <label for="" class="min-width bg-gray control-label">仓库：</label>
                    <label for="" class="control-label">{{editorInvList.whCheckVO.whName}}</label>
                  </div>
                </div>
                <div class="clearfix">
                  <div class="col-sm-12">
                    <table id="example4" class="table table-bordered table-striped marginNonde">
                      <thead>
                      <tr>
                        <th>商品信息</th>
                        <th>货架位</th>
                        <th>库存</th>
                        <th style="width: 15%">盘点数</th>
                        <th>盘点误差</th>
                        <th style="width: 20%">备注</th>
                      </tr>
                      </thead>
                      <tbody>
                      <tr v-if="editorInvList.whCheckListVOS.length == 0">
                        <td colspan="6">
                          没有数据
                        </td>
                      </tr>
                      <tr v-else v-for="editorInvListItem in editorInvList.whCheckListVOS">
                        <td class="text-left" style="padding-left: 20px;">
                          <img v-if="editorInvListItem.goodPic" :src="editorInvListItem.goodPic" alt="">
                          <img v-else src="@/assets/noPic.png" alt="">
                          <label class="m-l-15">
                            <p>
                              <label>商品编码：</label>
                              <span class="ellipsis">{{editorInvListItem.sku}}</span>
                            </p>
                            <p>
                              <label>商品名称：</label>
                              <span class="ellipsis">{{editorInvListItem.brandName}}</span>
                            </p>
                            <p>
                              <label>品牌名称：</label>
                              <span class="ellipsis">{{editorInvListItem.goodName}}</span>
                            </p>
                          </label>
                        </td>
                        <td>
                          <p v-for="stockVOsItem in editorInvListItem.stockVOs" class="lh-40">
                            {{stockVOsItem.stockNo}}
                          </p>
                        </td>
                        <td>
                          <p v-for="stockVOsItem in editorInvListItem.stockVOs" class="lh-40">
                            {{stockVOsItem.stockQty}}
                          </p>
                        </td>
                        <td>
                          <p v-for="stockVOsItem in editorInvListItem.stockVOs" class="m-tb-3">
                            <input type="text" :disabled="isReadonly" class="form-control" @blur="computeTask"
                                   v-model="stockVOsItem.sendQty">
                          </p>

                        </td>
                        <td>
                          <p v-if="editorInvListItem.errorNum == 0" class="lh-40 text-blue">
                            盘平
                          </p>
                          <p v-if="editorInvListItem.errorNum > 0" class="lh-40 text-green">
                            盘亏-{{editorInvListItem.errorNum}}
                          </p>
                          <p v-if="editorInvListItem.errorNum < 0" class="lh-40 text-danger">
                            盘盈+{{Math.abs(editorInvListItem.errorNum)}}
                          </p>
                        </td>
                        <td>
                          <input type="text" :disabled="isReadonly" class="form-control"
                                 v-model="editorInvListItem.note">
                        </td>
                      </tr>
                      </tbody>
                    </table>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="modal-footer">
            <button v-show="!isReadonly" type="button" class="btn btn-primary" @click="submitEditorTake('2')"
                    data-dismiss="modal">保存
            </button>
            <button v-show="!isReadonly" type="button" class="btn btn-primary" @click="submitEditorTake('3')"
                    data-dismiss="modal">结束盘点
            </button>
            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
          </div>
        </div>
        <!-- /.modal-content -->
      </div>
      <!-- /.modal-dialog -->
    </div>
    <!-- /.modal -->

  </section>
</template>

<script>
  import instance from '@/api/index.js'
  import pagination from '@/components/common/pageComponent'

  export default {
    name: "inventory",
    data() {
      return {
        title: '',
        takeStockStatus: [{name: "全部"}, {name: "已取消", status: "0"}, {
          name: "未开始",
          status: "1"
        }, {name: "进行中", status: "2"}, {name: "已结束", status: "3"}],//盘点状态
        inventoryLists: {},//盘点单列表
        inquireInventory: {},//盘点单查询传参T
        analysisTakeStockStatus: new Map,//盘点状态解析
        userPrivilegeWarehouse: [],//获取用户可查询仓库
        takeStockInfo: {},//创建盘点单信息展示
        submitWhParam: {
          whCheck: {
            whId: null
          }
        },//创建盘点单传参
        activeClass: {
          storckActive: -1,
          inventoryActive: 0,
        },
        current: 1,//当前页面
        total: 0,//总页数
        display:10,//显示条数
        checkType: 1,//盘点类型1整仓盘点 2库位盘点 3sku 盘点
        shelfId: null,//货架id查询
        stockId: null,//库位id查询
        shelfSelectList: [],//货架下拉框数据
        stockSelectList: [],//获取库位下拉框
        editoRoAdd: false,//盘点单显示
        editorInvList: {
          whCheckListVOS: [],
          whCheckVO: {},
        },//编辑获取的数据
        editorInvParma: {},//编辑保存数据
        totalStock: null,//库存总量
        errorNum: 10,//盘点误差
        isReadonly: false,
        ongoing: 0,
        show:false,//loading
      }
    },
    methods: {
      //消息弹窗提示
      alertMessage: function (value) {
        let type = 'success'
        if (!value.success) {
          type = 'error'
        }
        this.$message({
          showClose: false,
          message: value.msg,
          type: type
        });
      },
      // 获取分页
      pagechange: function (currentPage) {
        this.current = currentPage;
        this.getInventoryLists();
      },

      //获取盘点单列表
      getInventoryLists: function () {
        let vm = this;
        vm.inquireInventory.page = vm.current;
        vm.inquireInventory.rows = 20;
        vm.display = vm.inquireInventory.rows;
        vm.show = true;
        instance({
          method: 'post',
          url: '/v1/whReceiptProcess/getWareHouserCheckList',
          data: vm.inquireInventory
        }).then(function (data) {
          vm.inventoryLists = data.data.data.list;
          vm.total = data.data.data.total;
          vm.show = false;
        })
      },

      //获取用户可查看仓库
      userPrivilegeWarehouseFn: function () {
        const vm = this;
        instance.post('/v1/warehouse/getUserWareHouseList').then(function (data) {
          vm.userPrivilegeWarehouse = data.data.data;
        })
      },

      //仓库位置查询
      chooseWarehouse: function (id, index) {
        this.activeClass.storckActive = index;
        this.inquireInventory.whId = id;
        this.getInventoryLists(1);
      },

      //盘点状态查询
      chooseTakeStockStatus: function (status, index) {
        this.activeClass.inventoryActive = index;
        this.inquireInventory.status = status;
        this.getInventoryLists(1);
      },

      //盘点单编号查询
      searchInventoryKey: function (e) {
        this.activeClass.storckActive = -1;
        this.inquireInventory.whId = null;
        this.activeClass.inventoryActive = 0;
        this.inquireInventory.status = null;
        this.getInventoryLists();
      },

      //重置
      resetInvNo: function () {
        this.activeClass.storckActive = -1;
        this.inquireInventory.whId = null;
        this.activeClass.inventoryActive = 0;
        this.inquireInventory.status = null;
        this.inquireInventory.invNo = null;
        this.getInventoryLists();
      },

      //创建整仓盘点单选择仓库
      createWholeWarehouse: function () {
        let vm = this;
        instance.post('/v1/warehouse/getGoodsStockInfoList', {whId: vm.submitWhParam.whCheck.whId}).then(function (data) {
          console.log(data)
          vm.takeStockInfo = data.data.data;
        })
      },

      //点击创建整仓盘点单
      clickWholeWarehouse: function () {
        this.title = '仓库盘点单';
        this.submitWhParam.whCheck.whId = "";
        this.takeStockInfo = [];
        this.checkType = 1;
        this.editoRoAdd = false;
      },

      //获取货架下拉框
      getShelfSelect: function () {
        let vm = this;
        instance.post('/v1/warehouse/getShelfListByWhId/' + this.submitWhParam.whCheck.whId).then(function (data) {
          console.log(data.data.data)
          vm.shelfSelectList = data.data.data;
        })
      },

      //获取库位信息
      getStockSelect: function () {
        let vm = this;
        instance.post('/v1/warehouse/getStockListByShelfId/' + vm.shelfId).then(function (data) {
          if (data.data.success) {
            vm.stockSelectList = data.data.data;
          }
        })
      },

      //根据仓库货架获取商品信息
      getProductByShelf: function () {
        let vm = this;
        let getGoodsStockInfoListParma = {};
        if (!vm.shelfId) {
          vm.takeStockInfo = [];
          return;
        }
        getGoodsStockInfoListParma.shelfId = vm.shelfId;
        instance.post('/v1/warehouse/getGoodsStockInfoList', getGoodsStockInfoListParma).then(function (data) {
          if (data.data.success) {
            vm.takeStockInfo = data.data.data;
            vm.getStockSelect();
          }
        })
      },

      //根据仓库库位获取商品信息
      getProductByStock: function () {
        let vm = this;
        let getGoodsStockInfoListParma = {}
        getGoodsStockInfoListParma.stockId = vm.stockId;
        getGoodsStockInfoListParma.shelfId = vm.shelfId;
        instance.post('/v1/warehouse/getGoodsStockInfoList', getGoodsStockInfoListParma).then(function (data) {
          if (data.data.success) {
            console.log(data)
            vm.takeStockInfo = data.data.data;
          }
        })
      },

      //保存整仓库盘点单
      submitWholeWarehouse: function () {
        let vm = this;
        let submitWhole = {};
        if(vm.checkType == '1'){
          if (!vm.submitWhParam.whCheck.whId) {
            vm.$alert('请选择盘点仓库！', '提示', {
              confirmButtonText: '确定',
            });
            return;
          }
          $("#modal-default").modal('hide');
        }
        if(vm.checkType == '2'){
          if (!vm.submitWhParam.whCheck.whId) {
            vm.$alert('请选择盘点仓库！', '提示', {
              confirmButtonText: '确定',
            });
            return;
          }
          if (!vm.shelfId) {
            vm.$alert('请选择盘点货架！', '提示', {
              confirmButtonText: '确定',
            });
            return;
          }
          $("#modal-default").modal('hide');
        }

        submitWhole.whCheck = {
          "whId": vm.submitWhParam.whCheck.whId,
          "ongoing": 0,
          "totalNum": vm.takeStockInfo.length,
          "shelfId": vm.shelfId,
          "status": 1,
          "checkType": vm.checkType,
        };
        submitWhole.whCheckList = [];
        for (let i = 0; i < vm.takeStockInfo.length; i++) {
          for (let j = 0; j < vm.takeStockInfo[i].stockInfoVOS.length; j++) {
            let whCheckListObj = {};
            whCheckListObj.goodId = vm.takeStockInfo[i].goodsId;
            whCheckListObj.inventoryTotal = vm.takeStockInfo[i].stockInfoVOS[j].stockQty;
            whCheckListObj.stockNo = vm.takeStockInfo[i].stockInfoVOS[j].stockNo;
            whCheckListObj.stockId = vm.takeStockInfo[i].stockInfoVOS[j].stockId;
            whCheckListObj.status = 0;
            submitWhole.whCheckList.push(whCheckListObj)
          }
        }
        console.log(submitWhole)
        instance.post("/v1/whReceiptProcess/addWareHouserCheck", submitWhole).then(function (data) {
          console.log(data)
          vm.alertMessage(data.data);
          vm.getInventoryLists();
        })
      },

      //创建货架/库位盘点点
      createStorckOrShelf: function () {
        this.submitWhParam.whCheck.whId = "";
        this.shelfId = "";
        this.stockId = "";
        this.takeStockInfo = [];
        this.checkType = 2;
        this.title = '货架/库位盘点单';
        this.editoRoAdd = false;
      },

      //
      editorInventory: function (invNo, type) {
        let vm = this;
        if (type == '1') {
          this.title = '仓库盘点单'
          this.isReadonly = false;
        } else if (type == '2') {
          this.isReadonly = false;
          this.title = '货架/库位盘点单'
        }
        this.ongoing = 0;
        instance.post('/v1/whReceiptProcess/getWareHouserCheck', {invNo: invNo}).then(function (data) {
          if (data.data.success) {
            console.log(data)
            vm.editorInvList.whCheckListVOS = data.data.data.whCheckListVOS;
            vm.editorInvList.whCheckVO = data.data.data.whCheckVO;
            vm.computeTask();
            console.log(vm.editorInvList)
          }
        })
      },

      //盘点单详情
      detailsInventory: function (invNo, type) {
        if (type == '1') {
          this.isReadonly = true;
          this.title = '仓库盘点单'
        } else if (type == '2') {
          this.isReadonly = true;
          this.title = '货架/库位盘点单'
        }
        this.editorInventory(invNo);
      },

      //盘点误差计算
      computeTask: function () {
        for (let i = 0; i < this.editorInvList.whCheckListVOS.length; i++) {
          let current = this.editorInvList.whCheckListVOS[i];
          let stockVosSum = 0;
          let minusNum = 0;
          for (let j = 0; j < current.stockVOs.length; j++) {
            if (!current.stockVOs[j].sendQty) {
              stockVosSum = 0;
              minusNum = 0;
              this.editorInvList.whCheckListVOS[i].errorNum = null;
              return;
            } else if (current.stockVOs[j].sendQty < 0) {
              stockVosSum = 0;
              minusNum = 0;
              current.stockVOs[j].sendQty = minusNum;
              minusNum = current.stockVOs[j].stockQty - current.stockVOs[j].sendQty;
              stockVosSum = stockVosSum + parseInt(minusNum);
              this.editorInvList.whCheckListVOS[i].errorNum = stockVosSum;
              return;
            } else {
              console.log(JSON.stringify(current.stockVOs[j]));
              minusNum = current.stockVOs[j].stockQty - current.stockVOs[j].sendQty;
              stockVosSum = stockVosSum + parseInt(minusNum);
              this.editorInvList.whCheckListVOS[i].errorNum = stockVosSum;
            }
          }
        }
      },

      //盘点进度计算
      ongoingSetp: function () {
        this.ongoing = 0;
        console.log(JSON.stringify(this.editorInvList.whCheckListVOS));
        for (let i = 0; i < this.editorInvList.whCheckListVOS.length; i++) {
          //盘点误差不是0和库位盘点数不为null的时候当前盘点进度才算完成
          if (this.editorInvList.whCheckListVOS[i].errorNum || this.editorInvList.whCheckListVOS[i].errorNum == 0) {
            this.ongoing++;
          }
        }
      },

      //保存开始盘点继续盘点功能时候
      submitEditorTake: function (status) {
        let vm = this;
        vm.ongoingSetp();
        let takeStockObj = {};
        takeStockObj.whCheck = {
          "invNo": vm.editorInvList.whCheckVO.invNo,
          "whId": vm.editorInvList.whCheckVO.whId,
          "status": status,
          "ongoing": this.ongoing,
          "checkType": vm.editorInvList.whCheckVO.checkType,
          "shelfId": vm.editorInvList.whCheckVO.shelfId,
          "totalNum": vm.editorInvList.whCheckListVOS.length
        };
        takeStockObj.whCheckList = [];
        for (let i = 0; i < vm.editorInvList.whCheckListVOS.length; i++) {
          let current = vm.editorInvList.whCheckListVOS[i];
          for (let j = 0; j < current.stockVOs.length; j++) {
            let takeObj = {};
            takeObj.invNo = vm.editorInvList.whCheckVO.invNo;
            takeObj.goodId = current.goodId;
            takeObj.stockId = current.stockVOs[j].stockId;
            takeObj.stockNo = current.stockVOs[j].stockNo;
            takeObj.inventoryTotal = current.stockVOs[j].stockQty;
            takeObj.checkNum = current.stockVOs[j].sendQty;
            takeObj.note = current.note;
            takeObj.errorNum = current.errorNum;
            takeStockObj.whCheckList.push(takeObj)
          }
        }
        console.log(JSON.stringify(takeStockObj))
        instance.post('/v1/whReceiptProcess/updateWareHouserCheck', takeStockObj).then(function (data) {
          vm.alertMessage(data.data);
          vm.getInventoryLists();
          console.log(data)
        })

      },

      //取消盘点号
      deleteTaks: function (id) {
        let vm = this;
        vm.$confirm('此操作将取消此盘点单, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          instance.post('/v1/whReceiptProcess/cancelWareHouserCheck/' + id).then(function (data) {
            vm.alertMessage(data.data);
            vm.getInventoryLists();
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        })
      }

    },
    created() {
      this.submitWhParam.whCheck.whId = "请选择仓库";
      // 盘点状态1未开始 2进行中 3已结束 0已取消
      this.analysisTakeStockStatus.set("0", "已取消");
      this.analysisTakeStockStatus.set("1", "未开始");
      this.analysisTakeStockStatus.set("2", "进行中");
      this.analysisTakeStockStatus.set("3", "已结束");
      this.getInventoryLists();
      this.userPrivilegeWarehouseFn()
    },
    components: {
      pagination
    }
  }


</script>

<style scoped>
</style>
